from hyrobot.common import STEP, CHECK_POINT
from lib.webui import *
import pyautogui
import pytest
import time
import json
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

def turn_page(driver):
  time.sleep(1)
  driver.find_element(By.CSS_SELECTOR, ".el-input__inner").clear()
  driver.find_element(By.CSS_SELECTOR, ".el-input__inner").send_keys('3')
  time.sleep(1)
  driver.find_element(By.CSS_SELECTOR, "li.number:nth-child(2)").click()
  time.sleep(1)
  driver.find_element(By.CSS_SELECTOR, ".el-icon-arrow-right").click()
  time.sleep(1)
  driver.find_element(By.CSS_SELECTOR, ".el-icon-arrow-left").click()

def download_module(driver):
  time.sleep(2)
  driver.find_element(By.CSS_SELECTOR,
                           ".el-card__body > ul:nth-child(2) > li:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > button:nth-child(1) > span:nth-child(1)").click()
  pyautogui.press('down')
  pyautogui.press("enter", presses=6)

def check_detail(driver):
  # 点击查看详情
  time.sleep(1)
  driver.find_element(By.XPATH,
                      "/html/body/div/div/div[2]/section/div[1]/div/ul/li[1]/div/div/div[2]/button[3]/span"
                      ).click()
  time.sleep(1)
  temp_first_word = driver.find_element(By.XPATH,
                                        "/html/body/div[1]/div/div[2]/section/div[1]/div/div[2]/div/div[2]/div[1]/span[1]"
                                        ).text
  # close may
  time.sleep(1)
  driver.find_element(By.XPATH,
                      "/html/body/div[1]/div/div[2]/section/div[1]/div/div[2]/div/div[3]/span/button/span"
                      ).click()
  return temp_first_word

def commit_apply(driver):
  # 提交申请
  time.sleep(1)
  driver.find_element(By.CSS_SELECTOR,
                           ".el-card__body > ul:nth-child(2) > li:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > button:nth-child(2) > span:nth-child(1)"
                           ).click()
  time.sleep(2)
  driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(1) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "12312341234")
  driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(2) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "xiangmu")
  driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(3) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "danwei")
  driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(4) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "tom")
  driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(5) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "12312341234")
  driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(6) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "tom")
  driver.find_element(By.CSS_SELECTOR,
                           "div.el-form-item:nth-child(7) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)").send_keys(
    "12341234123")
  driver.find_element(By.CSS_SELECTOR, ".el-button--small > span:nth-child(1)").click()
  pyautogui.press("shift")
  pyautogui.typewrite(r"D:\dasan2\shixu\files\mcode.csv")
  # 需要多按几下
  while 1:
    pyautogui.press("enter", presses=1)
    # pyautogui.press("enter", presses=6)
    try:
      driver.find_element(By.CSS_SELECTOR, ".el-icon-document")
      break
    except NoSuchElementException:
      print("回车没有按够")
      continue

  time.sleep(3)
  driver.find_element(By.CSS_SELECTOR, ".next-step").click()
  time.sleep(2)
  driver.find_element(By.CSS_SELECTOR, ".el-icon-arrow-down").click()
  time.sleep(2)
  driver.find_element(By.XPATH,
                           "/html/body/div[2]/div[1]/div[1]/div[1]/ul/li/label/span/span").click()
  driver.execute_script("window.scrollTo(0,79.19999694824219)")
  driver.find_element(By.CSS_SELECTOR, ".el-form-item").click()
  driver.find_element(By.CSS_SELECTOR, ".next-step").click()

class c73248b:
    name = '验证所有界面存在且可操作产品列表部分-c73248b'  # 测试用例名字

    # 初始化方法
    def setup(self):
        STEP(1, '登陆网站')
        driver = open_browser()
        mgr_login(driver)

    # 清除方法
    def teardown(self):
        driver = get_global_webdriver()
        driver.quit()

    # 测试用例步骤
    def teststeps(self):
        # 获取webdriver对象 对应 已经登录好的浏览器
        driver = get_global_webdriver()
        # driver = webdriver.Firefox()

        STEP(2,"下载模板")
        download_module(driver)

        STEP(3,"查看详情")
        got_first_word = check_detail(driver)
        expected_first_word = "版本："
        CHECK_POINT("提示信息是否一致", got_first_word == expected_first_word)

        STEP(4,"页面跳转")
        turn_page(driver)

        STEP(2,"申请试用")
        commit_apply(driver)
